/**匹配界面 */
class macthRoom extends eui.Component {
	/**取消匹配按钮 */
	private cancel: eui.Button;
	/**开始游戏按钮 */
	private fightStart: eui.Button;
	/**匹配按钮 */
	private invitation: eui.Button;
	/**头像1 */
	private head1: eui.Image;
	/**头像2 */
	private head2: eui.Image;
	/**名字1 */
	private name1: eui.Label;
	/**名字2 */
	private name2: eui.Label;
	/**舌头装饰 */
	private tongue: eui.Image;
	/**房间人数 */
	private roomNumber: number;



	public constructor() {
		super();
		this.skinName = "resource/assets/skins/MatchRoom.exml";
		//适配
		st_config.setScreenAuto(this);
		this.init();
	}
	/**初始化 */
	public init() {
		if (DataProl.loginType == 2) {
			this.invitation.visible = false;
			this.fightStart.visible = false;
			this.tongue.visible = false;
			this.head2.touchEnabled = true;
			//this.cancel.visible = false;
		} else {
			this.fightStart.visible = true;
			this.head2.touchEnabled = false;
		}
		this.ShowMsg({
			name: gameGlobal.global.playerName,
			headUrl: weixinManager.getInstance().getUserInfo().avatarUrl,
			sex: weixinManager.getInstance().getUserInfo().gender
		});
		this.fightStart.addEventListener(egret.TouchEvent.TOUCH_TAP, this.gameStart, this);
		this.invitation.addEventListener(egret.TouchEvent.TOUCH_TAP, this.Invitation, this);
		this.cancel.addEventListener(egret.TouchEvent.TOUCH_TAP, this.cancelInvitation, this);
	}
	/**取消匹配 */
	public cancelInvitation() {
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sRoomExit = new awesomepackage.c2sRoomExit();
		m_c2sRoomExit.account = DataProl.account;
		m_c2sRoomExit.invoteRoomId = DataProl.waitRoomId;
		m_AwesomeMessage.data = awesomepackage.c2sRoomExit.encode(m_c2sRoomExit).finish();
		m_AwesomeMessage.meesageId = 2004;
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkMananger.getInstance().SendBitMeesage(byte2);
	}
	/**房间解散 */
	public dissolvedRoom(result) {
		console.log("退出房间");
		DataProl.loginType = 1;
		DataProl.waitRoomId = null;
		this.parent.removeChild(this);

	}
	/**可以开始游戏 */
	public CanStartGame() {
		if (DataProl.loginType == 2) {
			return;
		}
		this.fightStart.visible = true;
	}


	/**游戏开始 */
	public gameStart() {
		if (this.roomNumber == 1) {
			DataProl.login_main_handle.initializeModeSelection();
			DataProl.isSingleGame = true;
			let value = 'userid=' + DataProl.account;
			MiniNet.post(DataProl.loginaddr, '/wechat/getTopPlayer', value, DataProl.login_main_handle, DataProl.login_main_handle.singMatch);
			this.cancelInvitation();
			return;
		}
		if (DataProl.waitRoomId == null) {
			return;
		}
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sRoomStart = new awesomepackage.c2sRoomStart();
		m_c2sRoomStart.invoteRoomId = DataProl.waitRoomId;
		m_AwesomeMessage.data = awesomepackage.c2sRoomStart.encode(m_c2sRoomStart).finish();
		m_AwesomeMessage.meesageId = 2003;
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkMananger.getInstance().SendBitMeesage(byte2);
	}
	/**邀请好友*/
	public async Invitation() {
		if (DataProl.waitRoomId == null) {
			return;
		}
		await platform.activeShare(DataProl.waitRoomId);
	}
	/**清空好友消息 */
	public clearUserMsg() {
		if (DataProl.loginType == 2 && this.roomNumber == 2) {
			this.cancelInvitation();
		}
		if (DataProl.loginType == 1) {
			this.invitation.visible = true;
		}
		this.roomNumber == 1;
		console.log("删除好友的头像");
		this.name2.text = null;
		this.head2.source = null;
		this.tongue.visible = false;

	}
	/**显示头像 */
	public ShowMsg(data) {
		/**探测性别类型 */
		let sex = Object.prototype.toString.call(data.sex);
		if (sex == "Number") {
			if (data.sex == 1) {
				sex = '男';
			} else {
				sex = '女';
			}
		} else if (sex == "Boolean") {
			if (data.sex) {
				sex = '男';
			} else {
				sex = '女';
			}
		} else {
			sex = "未知";
		}
		let msg = {
			name: data.name,
			headUrl: data.headUrl,
			sex: sex
		}
		let number = msg.name == gameGlobal.global.playerName ? 1 : 2;
		this.roomNumber = number;
		/**头像图片 */
		var headImg: eui.Image;
		var nameLab: eui.Label;
		if (number == 1) {
			console.log("加载自己的头像");
			headImg = this.head1;
			nameLab = this.name1;
			this.tongue.visible = false;
		} else if (number == 2) {
			console.log("加载好友的头像");
			headImg = this.head2;
			nameLab = this.name2;
			this.invitation.visible = false;
			this.tongue.visible = true;
		}
		nameLab.text = msg.name;
		if (msg.headUrl != "text" && msg.headUrl != null) {
			//headImg.source = msg.headUrl;
		}
	}
}